﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ORM
{
    internal static class UserEF
    {
        static MainModelContainer context = null;
        //new MainModelContainer();
        static MainModelContainer GetContext()
        {
            if (context == null) context = new MainModelContainer(); return context;
        }

        public static User Retrieve(User UserInfo)
        {
            try
            {

                User find;
                find = GetContext().Users.FirstOrDefault(user => user.Login == UserInfo.Login);
                return find;
            }
            catch
            {
                return null;
            }
        }

        public static User RetrieveID(User UserInfo)
        {
            try
            {

                User find;
                find = GetContext().Users.FirstOrDefault(user => user.Id == UserInfo.Id);
                return find;
            }
            catch
            {
                return null;
            }
        }
        public static bool Create(User UserInfo)
        {

            try
            {

                GetContext().Users.AddObject(UserInfo);
                GetContext().SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Delete(User UserInfo)
        {

            try
            {
                UserInfo = RetrieveID(UserInfo);


                GetContext().Users.DeleteObject(UserInfo);
                GetContext().SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Update(User UserInfo)
        {

            User foo = RetrieveID(UserInfo);

            try
            {
                foo.BDate = UserInfo.BDate;
                foo.Email = UserInfo.Email;
                foo.FName = UserInfo.FName;
                foo.Login = UserInfo.Login;
                foo.Password = UserInfo.Password;
                foo.Phone = UserInfo.Phone;
                foo.Role = UserInfo.Role;
                foo.Salt = UserInfo.Salt;
                foo.SName = UserInfo.SName;


                GetContext().SaveChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    internal class AdminDataService : IAdminDataService
    {
        public User GetUserId(int id)
        {
            User foo = new User();
            foo.Id = id;
            foo = UserEF.RetrieveID(foo);
            return foo;
        }

        public User GetUserLogin(string login)
        {
            User foo = new User();
            foo.Login = login;
            foo = UserEF.Retrieve(foo);
            return foo;
        }

        public List<User> GetAllUsers()
        {
            List<User> Users = new List<User>();

            using (MainModelContainer context = new MainModelContainer())
            {
                Users = context.Users.ToList();
            }

            return Users;
        }

        public bool DropUsers()
        {
            try
            {

                foreach (User UserInfo in GetAllUsers())
                {
                    UserEF.Delete(UserInfo);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }

    internal class ClientDataService : IClientDataService
    {
        private MainModelContainer context;

        private MainModelContainer Context
        {
            get
            {
                if (context == null)
                    context = new MainModelContainer();
                return context;
            }
        }

        public List<Tour> GetAllTours()
        {
            List<Tour> Tours = new List<Tour>();

            Tours = Context.Tours.ToList();

            return Tours;
        }

        public List<Course> GetAllCourses()
        {
            List<Course> Courses = new List<Course>();

            Courses = Context.Courses.ToList();

            return Courses;
        }

        public Tour GetTourById(int Id)
        {
            Tour foo = new Tour();
            foo.Id = Id;
            return ClientEF.RetrieveID(foo);
        }
    }


    internal static class ClientEF
    {
        static MainModelContainer context = null;
        //new MainModelContainer();
        static MainModelContainer GetContext()
        {
            if (context == null) context = new MainModelContainer(); return context;
        }

        public static Tour Retrieve(Tour TourInfo)
        {
            try
            {

                Tour find;
                find = GetContext().Tours.FirstOrDefault(tour => tour.Course.Title == TourInfo.Course.Title);
                return find;
            }
            catch
            {
                return null;
            }
        }

        public static Tour RetrieveID(Tour TourInfo)
        {
            try
            {

                Tour find;
                find = GetContext().Tours.FirstOrDefault(Tour => Tour.Id == TourInfo.Id);
                return find;
            }
            catch
            {
                return null;
            }
        }

        public static bool Create(Tour TourInfo)
        {

            try
            {

                GetContext().Tours.AddObject(TourInfo);
                GetContext().SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static bool Delete(Tour TourInfo)
        {

            try
            {
                TourInfo = RetrieveID(TourInfo);

                GetContext().Tours.DeleteObject(TourInfo);
                GetContext().SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}